package com.wzh.house.controller;

import java.util.Arrays;
import java.util.Map;

import com.wzh.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.house.entity.NewUserAttentionEntity;
import com.wzh.house.service.NewUserAttentionService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-03-22 19:04:17
 */
@RestController
@RequestMapping("house/newuserattention")
public class NewUserAttentionController {
    @Autowired
    private NewUserAttentionService newUserAttentionService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = newUserAttentionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{newId}")
    public R info(@PathVariable("newId") Integer newId){
		NewUserAttentionEntity newUserAttention = newUserAttentionService.getById(newId);

        return R.ok().put("newUserAttention", newUserAttention);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody NewUserAttentionEntity newUserAttention, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token!=null) {
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null&&"user".equals(claims.get("roles"))) {
                newUserAttention.setUserId(Integer.valueOf(claims.getId()));
                if (newUserAttention.getState()){
                    //添加
                    newUserAttentionService.add(newUserAttention);
                    return R.ok().put("state",false);
                }else {
                    //删除
                    newUserAttentionService.del(newUserAttention);
                    return R.ok().put("state",true);
                }
            }
        }
		return R.error("用户未登录");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody NewUserAttentionEntity newUserAttention){
		newUserAttentionService.updateById(newUserAttention);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] newIds){
		newUserAttentionService.removeByIds(Arrays.asList(newIds));

        return R.ok();
    }

}
